/*
 * gastosIngresos.java
 *
 * Created on 3 de septiembre de 2008, 04:49 PM
 */

package administrador;

import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 *
 * @author  daniel1
 */
public class gastosIngresos extends javax.swing.JFrame {
Conexion conexion;
int bodega;
Usuario user;
String fecha;        
    /** Creates new form gastosIngresos */
    public gastosIngresos(Conexion c,int bodega,Usuario user) {
        conexion=c;
        this.bodega=bodega;
        this.user=user;
        initComponents();
        setBounds(100,100,479,398);
        setVisible(true);
        cargoResponsable();
        fecha=new java.sql.Date(System.currentTimeMillis()).toString();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        cb_salidas = new javax.swing.JComboBox();
        txt_monto = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        txt_montoDos = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jButton3 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txt_resp = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(administrador.AdministradorApp.class).getContext().getResourceMap(gastosIngresos.class);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPanel1.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jPanel1.border.titleFont"), resourceMap.getColor("jPanel1.border.titleColor"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel2.setName("jPanel2"); // NOI18N

        cb_salidas.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Pago de Sueldos", "Pago IESS", "Pago Comisiones Vendedores", "Gastos Varios", "Intereses Pagados" }));
        cb_salidas.setName("cb_salidas"); // NOI18N

        txt_monto.setText(resourceMap.getString("txt_monto.text")); // NOI18N
        txt_monto.setName("txt_monto"); // NOI18N
        txt_monto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txt_montoKeyTyped(evt);
            }
        });

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(cb_salidas, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txt_monto, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(29, 29, 29)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txt_monto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(1, 1, 1)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(cb_salidas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(49, 49, 49))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel3.setName("jPanel3"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        txt_montoDos.setText(resourceMap.getString("txt_montoDos.text")); // NOI18N
        txt_montoDos.setName("txt_montoDos"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jComboBox2.setName("jComboBox2"); // NOI18N

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txt_montoDos, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jComboBox2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(44, 44, 44)
                        .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addContainerGap(313, Short.MAX_VALUE))))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txt_montoDos))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        txt_resp.setEditable(false);
        txt_resp.setText(resourceMap.getString("txt_resp.text")); // NOI18N
        txt_resp.setName("txt_resp"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel2)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel9)
                        .addGap(18, 18, 18)
                        .addComponent(txt_resp, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel9)
                        .addComponent(txt_resp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void cargoResponsable(){
    txt_resp.setText(user.getNombre());
    }
    
    
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    //valido que no puedan ingresar mas que numeros
    double valor=0;
    try{
    valor = Double.parseDouble(txt_monto.getText().replaceAll(",","."));
    
    
    
    int seleccion = cb_salidas.getSelectedIndex();
    if (seleccion >= 0) {
        if (seleccion == 0) {
            //Registro de Pago de Sueldos:
           //System.out.println("Registro de Pago de Sueldos: " + valor);
            asientoPagoSueldo(String.valueOf(valor));
        } else if (seleccion == 1) {
            //Pago IESS
           //System.out.println("Pago IESS: " + valor);
            asientoPagoIess(String.valueOf(valor));
        } else if (seleccion == 2) {
            //Pago de Comisiones:
           //System.out.println("Pago de Comisiones: " + valor);
            asientoPagoComisiones(String.valueOf(valor));
        } else if (seleccion == 3) {
            //Gastos Varios:
           //System.out.println("Gastos Varios: " + valor);
            asientoGastosVarios(String.valueOf(valor));
        } else if (seleccion == 4) {
            //Intereses Pagados:
           //System.out.println("Intereses Pagados: " + valor);
            asientoInteresesPagados(String.valueOf(valor));
        }  
        
        JOptionPane.showMessageDialog(this,"Asiento Ingresado Exitosamente","Informacion",JOptionPane.INFORMATION_MESSAGE);
        limpiar();
    }else{
    JOptionPane.showMessageDialog(this,"Seleccione almenos una opcion de registro","Informacion",JOptionPane.INFORMATION_MESSAGE);
    }
    }catch(Exception e){
    JOptionPane.showMessageDialog(this,"El valor ignresado no es correcto","Informacion",JOptionPane.INFORMATION_MESSAGE);
    }
}//GEN-LAST:event_jButton1ActionPerformed

    public void limpiar(){
    txt_monto.setText("0.00");
    txt_montoDos.setText("0.00");
    }


    public void asientoPagoSueldo(String valor){
            String numeroDiario=numeroDiario();
            this.asiento(numeroDiario,fecha,"Asiento de Pagos de Sueldos");
            String numeroAsiento=numeroAsiento();
            asientoDeatlle(numeroAsiento,numeroDiario,"1","1","4","0",valor,"0");   //Sueldos y Salarios
            asientoDeatlle(numeroAsiento,numeroDiario,"1","1","1","0","0",valor);   //Asiento de Caja  debe
    }


    public void asientoPagoIess(String valor){
            String numeroDiario=numeroDiario();
            this.asiento(numeroDiario,fecha,"Asiento de Pagos de IESS");
            String numeroAsiento=numeroAsiento();
            asientoDeatlle(numeroAsiento,numeroDiario,"2","1","4","0",valor,"0");   //Pago al IESS
            asientoDeatlle(numeroAsiento,numeroDiario,"1","1","1","0","0",valor);   //Asiento de Caja  debe
    }
    
    
    

    public void asientoPagoComisiones(String valor){
            String numeroDiario=numeroDiario();
            this.asiento(numeroDiario,fecha,"Pagos de Comisiones Ganadas a Vendedores");
            String numeroAsiento=numeroAsiento();
            asientoDeatlle(numeroAsiento,numeroDiario,"3","1","4","0",valor,"0");   //Pago de Comisiones Ganadas
            asientoDeatlle(numeroAsiento,numeroDiario,"1","1","1","0","0",valor);   //Asiento de Caja  debe
    }

    
    public void asientoGastosVarios(String valor){
            String numeroDiario=numeroDiario();
            this.asiento(numeroDiario,fecha,"Pagos Gastos Varios");
            String numeroAsiento=numeroAsiento();
            asientoDeatlle(numeroAsiento,numeroDiario,"10","1","4","0",valor,"0");   //Pago Gastos Varios
            asientoDeatlle(numeroAsiento,numeroDiario,"1","1","1","0","0",valor);   //Asiento de Caja  debe
    }
    
    public void asientoInteresesPagados(String valor){
            String numeroDiario=numeroDiario();
            this.asiento(numeroDiario,fecha,"Registro de Intereses Pagados");
            String numeroAsiento=numeroAsiento();
            asientoDeatlle(numeroAsiento,numeroDiario,"11","1","4","0",valor,"0");   //Pago Gastos Varios
            asientoDeatlle(numeroAsiento,numeroDiario,"1","1","1","0","0",valor);   //Asiento de Caja  debe
    }

    public String numeroDiario(){
         String numero=""; 
         try{
            java.sql.ResultSet r=conexion.consultarBase("select  cod_diario from diario where fecha_diario=(select max(fecha_diario) from diario) limit 1");
            r.next();
            numero=r.getString(1);
          }catch(SQLException f){}
         return numero;
    }
    public String numeroAsiento(){
         String numero=""; 
         try{
            java.sql.ResultSet r=conexion.consultarBase("select max(cod_asiento) from asiento");
            r.next();
            numero=r.getString(1);
          }catch(SQLException f){}
         return numero;
    }
    public void asiento(String numeroDiario,String fecha,String referencia){
        conexion.insertarDatos("insert into asiento(cod_diario,fecha_dia,referencia) values("+numeroDiario+",'"+fecha+"','"+referencia+"')");
    }
    public void asientoDeatlle(String numeroAsiento,String numeroDiario,String n3,String n2,String n1,String parcial,String debe,String haber){
        String consultaDtlAs= "insert into dtl_asiento "+
                              "(cod_diario,cod_asiento,cod_tercernivel,cod_segundonivel,cod_primernivel,parcial,debe,haber) " +
                              "values ("+numeroDiario+","+numeroAsiento+","+n3+","+n2+","+n1+","+parcial+","+debe+","+haber+")";
        conexion.insertarDatos(consultaDtlAs);
    }



private void txt_montoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_montoKeyTyped
// TODO add your handling code here:
    
    
}//GEN-LAST:event_txt_montoKeyTyped

    /**
    * @param args the command line arguments
    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new gastosIngresos().setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cb_salidas;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField txt_monto;
    private javax.swing.JTextField txt_montoDos;
    private javax.swing.JTextField txt_resp;
    // End of variables declaration//GEN-END:variables

}
